package edu.dali.enterprise_test.kits;

import edu.dali.enterprise_test.model.dto.SummaryDto;

import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class ExcelUtils {

    public ByteArrayOutputStream getSummaryData(Map<String, Object> res) {
        Map<String , Object> map = new HashMap<>();

        SummaryDto summaryDto = (SummaryDto) res.get("summary");


        Integer initStock = 0;
        Integer endStock = 0;
        Integer invCount = 0;
        Integer invRedCount = 0;
        Integer backInvCount = 0;
        Integer buyInvCount = 0;
        Integer invVoidCount = 0;
        Integer invRedVoidCount = 0;
        Integer returnInvCount = 0;
        Integer distributeInvCount = 0;

        double summaryAmount = 0;
        double summaryVoidAmount = 0;
        double summaryRedVoidAmount = 0;
        double summaryRedAmount = 0;

        double summaryVoidTax = 0;
        double summaryTax = 0;
        double summaryRedVoidTax = 0;
        double summaryRedTax = 0;


        for (int i = 0; i < summaryDto.getInvoiceName().size(); i++) {
            initStock += Integer.parseInt(summaryDto.getInitStock().get(i));
            endStock += Integer.parseInt(summaryDto.getEndStock().get(i));
            invCount += Integer.parseInt(summaryDto.getInvCount().get(i));
            invRedCount += Integer.parseInt(summaryDto.getInvRedCount().get(i));
            backInvCount += Integer.parseInt(summaryDto.getBackInvCount().get(i));
            buyInvCount += Integer.parseInt(summaryDto.getBuyInvCount().get(i));
            invVoidCount += Integer.parseInt(summaryDto.getInvVoidCount().get(i));
            invRedVoidCount += Integer.parseInt(summaryDto.getInvRedVoidCount().get(i));
            returnInvCount += Integer.parseInt(summaryDto.getReturnInvCount().get(i));
            distributeInvCount += Integer.parseInt(summaryDto.getDistributeInvCount().get(i));

            summaryVoidAmount += Double.parseDouble(summaryDto.getSummaryVoidAmount().get(i));
            summaryAmount += Double.parseDouble(summaryDto.getSummaryAmount().get(i));
            summaryRedVoidAmount = Double.parseDouble(summaryDto.getSummaryRedVoidAmount().get(i));
            summaryRedAmount += Double.parseDouble(summaryDto.getSummaryRedAmount().get(i));

            summaryVoidTax += Double.parseDouble(summaryDto.getSummaryVoidTax().get(i));
            summaryTax += Double.parseDouble(summaryDto.getSummaryTax().get(i));
            summaryRedVoidTax += Double.parseDouble(summaryDto.getSummaryRedVoidTax().get(i));
            summaryRedTax +=  Double.parseDouble(summaryDto.getSummaryRedTax().get(i));
        }
        map.put("initStock" , String.valueOf(initStock));
        map.put("endStock" , String.valueOf(endStock));
        map.put("invCount" , String.valueOf(invCount));
        map.put("invRedCount" , String.valueOf(invRedCount));
        map.put("backInvCount" , String.valueOf(backInvCount));
        map.put("buyInvCount" , String.valueOf(buyInvCount));
        map.put("invVoidCount" , String.valueOf(invVoidCount));
        map.put("invRedVoidCount" , String.valueOf(invRedVoidCount));
        map.put("returnInvCount" , String.valueOf(returnInvCount));
        map.put("distributeInvCount" , String.valueOf(distributeInvCount));

        map.put("summaryVoidAmount" , String.valueOf(summaryVoidAmount));
        map.put("summaryAmount" , String.valueOf(summaryAmount));
        map.put("summaryRedVoidAmount" , String.valueOf(summaryRedVoidAmount));
        map.put("summaryRedAmount" , String.valueOf(summaryRedAmount));
        BigDecimal amount = BigDecimal.valueOf(summaryVoidAmount).add(BigDecimal.valueOf(summaryAmount)).add( BigDecimal.valueOf(summaryRedVoidAmount)).add( BigDecimal.valueOf(summaryRedAmount));
        map.put("amount" , String.valueOf(amount));

        map.put("summaryVoidTax" , String.valueOf(summaryVoidTax));
        map.put("summaryTax" , String.valueOf(summaryTax));
        map.put("summaryRedVoidTax" , String.valueOf(summaryRedVoidTax));
        map.put("summaryRedTax" , String.valueOf(summaryRedTax));
        BigDecimal tax = BigDecimal.valueOf(summaryVoidTax).add(BigDecimal.valueOf(summaryTax)).add( BigDecimal.valueOf(summaryRedVoidTax)).add( BigDecimal.valueOf(summaryRedTax));
        map.put("tax" , String.valueOf(tax));


        ExcelTemplate et = ExcelTemplate.getInstants();
        et = et.readTemplateByClassPath("static/excel/template.xlsx");
        ByteArrayOutputStream ret = et.dataIterator(map);
        return ret;
    }
}
